package cn.hema.mapper;

import cn.hema.po.Book;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;

public interface BookMapper {
    // 1. 根据ID查询书籍
    @Select("SELECT * FROM book WHERE id = #{id}")
    Optional<Book> findById(@Param("id") Long id);

    @Insert("INSERT INTO book (title, author, isbn, stock) " +
            "VALUES (#{title}, #{author}, #{isbn}, #{stock})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void addBook(Book book);

    @Delete("DELETE FROM book WHERE id = #{id}")
    void deleteBook(Long id);

    @Update("UPDATE book " +
            "SET title = #{title}, author = #{author}, isbn = #{isbn}, stock = #{stock} " +
            "WHERE id = #{id}")
    void updateBook(Book book);

    @Select("SELECT * FROM book")
    List<Book> getAllBooks();
}
